using UnityEngine;
using UnityEditor;
using TMPro;

public class FontAtlasTextureHelper : EditorWindow
{
    [MenuItem("Tools/TextMeshPro/Fix Font Atlas Texture")]
    public static void FixFontAtlasTexture()
    {
        // 查找所有TMP_FontAsset
        string[] guids = AssetDatabase.FindAssets("t:TMP_FontAsset");
        
        foreach (string guid in guids)
        {
            string path = AssetDatabase.GUIDToAssetPath(guid);
            TMP_FontAsset fontAsset = AssetDatabase.LoadAssetAtPath<TMP_FontAsset>(path);
            
            if (fontAsset != null && fontAsset.atlasTextures != null)
            {
                foreach (var atlasTexture in fontAsset.atlasTextures)
                {
                    if (atlasTexture != null)
                    {
                        string texturePath = AssetDatabase.GetAssetPath(atlasTexture);
                        TextureImporter textureImporter = AssetImporter.GetAtPath(texturePath) as TextureImporter;
                        
                        if (textureImporter != null && !textureImporter.isReadable)
                        {
                            Debug.Log($"设置字体图集纹理为可读: {atlasTexture.name}");
                            textureImporter.isReadable = true;
                            textureImporter.SaveAndReimport();
                        }
                    }
                }
            }
        }
        
        Debug.Log("字体图集纹理检查完成！");
    }
    
    [MenuItem("Tools/TextMeshPro/Fix Specific Font Atlas")]
    public static void FixSpecificFontAtlas()
    {
        // 查找W120 SDF字体
        string[] guids = AssetDatabase.FindAssets("W120 SDF t:TMP_FontAsset");
        
        foreach (string guid in guids)
        {
            string path = AssetDatabase.GUIDToAssetPath(guid);
            TMP_FontAsset fontAsset = AssetDatabase.LoadAssetAtPath<TMP_FontAsset>(path);
            
            if (fontAsset != null && fontAsset.name.Contains("W120"))
            {
                Debug.Log($"处理字体: {fontAsset.name}");
                
                if (fontAsset.atlasTextures != null)
                {
                    foreach (var atlasTexture in fontAsset.atlasTextures)
                    {
                        if (atlasTexture != null)
                        {
                            string texturePath = AssetDatabase.GetAssetPath(atlasTexture);
                            TextureImporter textureImporter = AssetImporter.GetAtPath(texturePath) as TextureImporter;
                            
                            if (textureImporter != null)
                            {
                                Debug.Log($"当前纹理 {atlasTexture.name} 可读状态: {textureImporter.isReadable}");
                                
                                if (!textureImporter.isReadable)
                                {
                                    Debug.Log($"设置纹理为可读: {atlasTexture.name}");
                                    textureImporter.isReadable = true;
                                    textureImporter.SaveAndReimport();
                                }
                            }
                        }
                    }
                }
            }
        }
        
        Debug.Log("W120字体图集处理完成！");
    }
} 